// pages/search/search.js
import { request } from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 热搜榜数据
        hotList:[],
        // 用户输入的表单项数据
        searchContent:'',
        // 关键字模糊匹配的数据
        searchList:[],
        // 搜索历史记录
        historyList:[]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 获取初始化数据
        this.getInitData();
        // 获取历史记录
        this.getSearchHistory()
        
    },
    // 获取初始化的数据
   async getInitData(){
        let hotListData = await request('/music/condition',"GET",{isTop:1})
        let newArray =  hotListData.data.filter((item,index)=>{
            return index<=19;
        })
        this .setData({
            hotList:newArray
        })
    },
    // 获取本地历史记录的功能函数
    getSearchHistory(){
       let historyList= wx.getStorageSync('searchHistory')
       if(historyList.length){
           this.setData({
            historyList 
           })
       }
    },
    // 表单项内容发生改变的回调
    handleInputChange(event){
        if(event.detail.trim()==""){
          this.setData({
              searchList:[]
          })
        }
        this .setData({
            searchContent:event.detail.trim()
        })
    },
    // 获取搜索数据的功能函数
    async getSearchList(){
        // 发请求获取关键字模糊匹配数据
        let { searchContent, historyList }=this.data

        let searchListData = await request('/music/keyword',"GET",{keyword:searchContent})
        
        if(historyList.length>=10){
          historyList.pop()
          historyList.unshift(searchContent)
        }else{
          historyList.unshift(searchContent)
        }
        if(searchListData.data.length==0){
          wx.showToast({
            title: '暂未搜索到此歌曲',
            icon:"error"
          })
        }
        this .setData({
          searchList:searchListData.data,
          historyList
      })
        wx.setStorageSync('searchHistory', historyList)
    },
    // 删除搜索历史记录
    deleteSearchHistory(){
        wx.showModal({
          content: '确认删除吗？',
          success:(res)=>{
            if(res.confirm){
                this.setData({
                    historyList:[]
                })
                // 移除本地的历史记录缓存
                wx.removeStorageSync('searchHistory')
            }
          }
        })
       
    },
    async searchByHistory(e){
      let index = e.currentTarget.dataset.index
      let value = this.data.historyList[index]
      let searchListData = await request('/music/keyword',"GET",{keyword:value})
      if(searchListData.data.length==0){
        wx.showToast({
          title: '暂未搜索到此歌曲',
          icon:"error"
        })
      }
      this .setData({
        searchList:searchListData.data,
        searchContent:value
      })
    },
    toPlay(e){
      let index = e.currentTarget.dataset.index
        let currentMusic =  this.data.searchList[index]
        wx.navigateTo({
          url: '/pages/songDetail/songDetail',
          success: function(res) {
            res.eventChannel.emit('detail', { musicData:currentMusic})
          }
      })
    },
    hotPlay(e){
      let index = e.currentTarget.dataset.index
        let currentMusic =  this.data.hotList[index]
        wx.navigateTo({
          url: '/pages/songDetail/songDetail',
          success: function(res) {
            res.eventChannel.emit('detail', { musicData:currentMusic})
          }
      })
    }
})